package com.e3.basdoc.corp.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.corp.model.CorpConstant;
import com.e3.basdoc.corp.model.CorpFields;
import com.e3.basdoc.corp.model.CorpModel;
import com.e3.basdoc.currency.client.CurrencyRefDialog;
import com.e3.basdoc.region.client.RegionRefDialog;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyRadioGroup;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class CorpHelper extends TGFHelper<CorpModel>{

	public static CorpHelper INSTANCE = new CorpHelper();
	
	private String[] notNullFields = {CorpModel.CODE,CorpModel.NAME
			,CorpModel.SHORT_NAME,CorpFields.REGION
			,CorpFields.MAX_USERS,CorpFields.MAX_BRANCHS
			,CorpFields.CURRENCY,CorpFields.TYPE
			,};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private CorpHelper(){
		lengthMap.put(CorpModel.ID, 19);
		lengthMap.put(CorpModel.CODE, 20);
		lengthMap.put(CorpModel.NAME, 50);
		lengthMap.put(CorpModel.SHORT_NAME, 20);
		lengthMap.put(CorpModel.CONTACT_ADDRESS, 100);
		lengthMap.put(CorpModel.EMAIL, 50);
		lengthMap.put(CorpModel.CONTACT_PERSON, 50);
		lengthMap.put(CorpModel.CONTACT_WAY, 100);
		lengthMap.put(CorpModel.FAX, 20);
		lengthMap.put(CorpModel.BUSINESS_ADDRESS, 100);
		lengthMap.put(CorpModel.REGISTER_ADDRESS, 100);
		lengthMap.put(CorpModel.TELPHONE, 20);
		lengthMap.put(CorpModel.ZIPCODE, 20);
		lengthMap.put(CorpModel.REGISTER_NO, 30);
		lengthMap.put(CorpModel.TAX_NO, 30);
		lengthMap.put(CorpModel.LEGAL_PERSON, 20);
		lengthMap.put(CorpModel.LEGAL_PERSON_IDCARD, 20);
		lengthMap.put(CorpModel.REMARK, 100);

		lengthMap.put(CorpModel.MAX_USERS, 4);
		lengthMap.put(CorpModel.MAX_BRANCHS, 4);
		lengthMap.put(CorpModel.REMARK, 100);
		
	}


	public Field<?> createFormField(String name) {
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		
		if (name.equals(CorpFields.CODE) || name.equals(CorpFields.NAME) || name.equals(CorpFields.SHORT_NAME)
				|| name.equals(CorpFields.BUSINESS_ADDRESS) || name.equals(CorpFields.CONTACT_ADDRESS) || name.equals(CorpFields.TELPHONE)
				|| name.equals(CorpFields.EMAIL) || name.equals(CorpFields.ZIPCODE) || name.equals(CorpFields.FAX)
				|| name.equals(CorpFields.REGISTER_ADDRESS) || name.equals(CorpFields.REGISTER_NO) || name.equals(CorpFields.TAX_NO)
				|| name.equals(CorpFields.CONTACT_PERSON) || name.equals(CorpFields.CONTACT_WAY) || name.equals(CorpFields.LEGAL_PERSON)
				|| name.equals(CorpFields.LEGAL_PERSON_IDCARD)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CorpFields.MAX_USERS) || name.equals(CorpFields.MAX_BRANCHS)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createIntField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CorpFields.PARENT_CORP)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new CorpRefDialog(),isNotNull);
		}
		else if (name.equals(CorpFields.REGION)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new RegionRefDialog(),isNotNull);
		}
		else if (name.equals(CorpFields.CURRENCY)) {
			// TODO 调用货币选择对话框
			formField = MyFormFieldFactory.createRefField(name, dispName, new CurrencyRefDialog(),isNotNull);
		}
		else if (name.equals(CorpFields.TYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			// 企业法人（1）、分支机构（2）、个体（3）、其他（4）
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TYPE_LEGALPERSON, CorpFieldsConstant.INSTANCE.type_legal_person()));
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TYPE_BRANCH, CorpFieldsConstant.INSTANCE.type_branch()));
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TYPE_INDIVIDUAL, CorpFieldsConstant.INSTANCE.type_individual()));
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TYPE_OTHERS, CorpFieldsConstant.INSTANCE.type_others()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CorpFields.ESTABLISH_ACCOUNT)) {
			MyRadioGroup<Boolean> radio = MyFormFieldFactory.createRadioGroupField(name, dispName);
			radio.addRadio(CorpFieldsConstant.INSTANCE.establish_account_no(), false, true);
			radio.addRadio(CorpFieldsConstant.INSTANCE.establish_account_yes(), true, false);
			formField = radio;
			formField.setEnabled(false);
		}
		else if (name.equals(CorpFields.TAXPAYER_TYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TAXPAYER_COMMON, CorpFieldsConstant.INSTANCE.taxpayer_type_common()));
			listValueModels.add(new ListTypeModel<Integer>(CorpConstant.TAXPAYER_SMALL, CorpFieldsConstant.INSTANCE.taxpayer_type_small_scale()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CorpFields.FOUND_TIME) || name.equals(CorpFields.REGISTER_START_TIME)
				|| name.equals(CorpFields.REGISTER_END_TIME) || name.equals(CorpFields.START_TIME) || name.equals(CorpFields.END_TIME)) {
			formField = MyFormFieldFactory.createDateField(name, dispName,isNotNull);
		}
		else if (name.equals(CorpFields.REMARK)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CorpFields.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}
	

	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name : fNames) {
			Field<?> formField = createFormField(name);
			if (formField == null) {
				continue;
			}
			fields.add(formField);
		}
		return fields;
	}

	/**
	 * 设置地区之间的父子关系。
	 * 
	 * @param pCorp
	 * @param children
	 */
	public void setCorpRelations(CorpModel pCorp, List<CorpModel> children) {
		if(children==null){
			return;
		}
		for (CorpModel corpModel : children) {
			corpModel.setParentCorp(pCorp);
		}
	}

	public List<ColumnConfig> getCorpGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = CorpModel.CODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CorpModel.NAME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CorpModel.BUSINESS_ADDRESS;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CorpModel.CONTACT_ADDRESS;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CorpModel.ZIPCODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		return configs;
	}

	public String getFieldDisplayName(String methodName) {
		return CorpFieldsConstant.INSTANCE.getString(methodName);
	}
	
	public CorpModel createModelInstance() {
		return new CorpModel();
	}


	@Override
	public String getCodeFieldName() {
		return CorpModel.CODE;
	}


	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

}
